<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />

  <xsl:param name="path" />

  <xsl:template match="JDepend">
    <xmldata>

      <xsl:for-each select="./Packages/Package">
        <xsl:if test="count(error) = 0">
          <xsl:variable name="packagename" select="@name" />
          <xsl:variable name="afferentCouplings" select="./Stats/Ca" />
          <xsl:variable name="efferentCouplings" select="./Stats/Ce" />
          <xsl:variable name="abstractness" select="./Stats/A" />
          <xsl:variable name="instability" select="./Stats/I" />
          <xsl:variable name="distance" select="./Stats/D" />
          <xsl:variable name="cycles" select="count(//Cycles/Package[@Name=$packagename]/Package)" />

          <xsl:variable name="outboundpackages">
            <xsl:for-each select="./DependsUpon/Package">
              <xsl:variable name="attribute_name" select="@name" />
              <xsl:variable name="attribute_value" select="." />
              <xsl:value-of select="concat(., ',')" />
            </xsl:for-each>
          </xsl:variable>

          <xsl:variable name="outbound">
            <xsl:variable name="fullength" select="string-length($outboundpackages)" />
            <xsl:choose>
              <xsl:when test="$fullength&gt;'0'">
                <xsl:variable name="length" select="$fullength - 1" />
                <xsl:variable name="lastchar" select="substring($outboundpackages,$fullength, 1)" />
                <xsl:choose>
                  <xsl:when test="$lastchar = ','">
                    <xsl:value-of select="substring($outboundpackages, 1, $length)" />
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$outboundpackages" />
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
            </xsl:choose>
          </xsl:variable>

          <xsl:variable name="inboundpackages">
            <xsl:for-each select="./UsedBy/Package">
              <xsl:variable name="attribute_name" select="@name" />
              <xsl:variable name="attribute_value" select="." />
              <xsl:value-of select="concat(., ',')" />
            </xsl:for-each>
          </xsl:variable>

          <xsl:variable name="inbound">
            <xsl:variable name="fullength" select="string-length($inboundpackages)" />
            <xsl:choose>
              <xsl:when test="$fullength&gt;'0'">
                <xsl:variable name="length" select="$fullength - 1" />
                <xsl:variable name="lastchar" select="substring($inboundpackages,$fullength, 1)" />
                <xsl:choose>
                  <xsl:when test="$lastchar = ','">
                    <xsl:value-of select="substring($inboundpackages, 1, $length)" />
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$inboundpackages" />
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
            </xsl:choose>
          </xsl:variable>

          <xsl:variable name="suffix" select="translate($packagename , '.', '\')" />
          <xsl:variable name="full_path" select="concat($path,'\',$suffix)" />

          <entry sdt="Dependency" granularity="package" tool="JDepend" path="{$full_path}"
            package="{$packagename}" outbound="{$outbound}" inbound="{$inbound}"
            afferentCouplings="{$afferentCouplings}" efferentCouplings="{$efferentCouplings}"
            abstractness="{$abstractness}" instability="{$instability}" distance="{$distance}" 
            cycles="{$cycles}" />
        </xsl:if>
      </xsl:for-each>

    </xmldata>
  </xsl:template>

</xsl:stylesheet>